package itheima.factory;

import itheima.dao.BaseMetroDao;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;


public class MetroFactory {
    private static Properties prop;
    static {
        try {
            prop = new Properties();
            InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("dao.properties");
            prop.load(is);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static BaseMetroDao getDao()  {
        try {
            //1.获取className值
            String daoName = prop.getProperty("className");
            //2.通过反射获得对象
            Class clazz = Class.forName(daoName);
            Constructor constructor = clazz.getConstructor();
            //3.创建Dao对象
            BaseMetroDao md = (BaseMetroDao) constructor.newInstance();
            //4.返回Dao
            return md;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }
}
